<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item label="设备名称">
        <el-input
          v-model="formInline.equipmentName"
          placeholder="设备名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备位置">
        <el-select v-model="formInline.equipmentLocaId" placeholder="设备位置">
          <el-option label="请选择" value=""></el-option>
          <el-option label="1栋" value="1"></el-option>
          <el-option label="2栋" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="equipmentCoding" label="设备编码" align="center">
        </el-table-column>
        <el-table-column prop="equipmentName" label="设备名称" align="center">
        </el-table-column>
        <el-table-column prop="deviceType" label="设备类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.deviceType == 1">视频监控系统</span>
            <span v-if="scope.row.deviceType == 2">巡检感应系统</span>
          </template>
        </el-table-column>
        <el-table-column prop="equipmentModel" label="型号" align="center">
        </el-table-column>
        <el-table-column prop="equipmentBrand" label="品牌" align="center">
        </el-table-column>
        <el-table-column prop="equipmentLocaId" label="设备位置" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.equipmentLocaId == 1">1号电梯</span>
            <span v-if="scope.row.equipmentLocaId == 2">2号电梯</span>
          </template>
        </el-table-column>
        <el-table-column prop="mainteCycle" label="维护周期" align="center">
        </el-table-column>
        <el-table-column prop="mainteDepartId" label="维护部门" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.mainteDepartId == 1">广州公司01</span>
            <span v-if="scope.row.mainteDepartId == 2">北京公司02</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="设备在线状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.state == true">在线</span>
            <span v-if="scope.row.state == false" style="color: red">离线</span>
          </template>
        </el-table-column>
        <el-table-column label="二维码" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="creatQrCode(scope.row.equipmentName)"
              >点击</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: green"
              @click="examine(scope.row)"
              >配置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>

    <el-dialog title="详情信息" :visible.sync="dialogFormVisible1">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        style="width: 600px; margin: 2px auto"
      >
        <el-form-item label="设备状态" prop="state">
          <el-input v-model="form.state"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="二维码" :visible.sync="dialogTableVisible" width="300px">
      <div class="qrcode divqr" ref="qrCodeUrl"></div>
    </el-dialog>
  </div>
</template>
<script>
import $ from "jquery";
import QRCode from "qrcodejs2";
import { GetShowEquipmentLedger } from "@/api/assetManage/EquipmentLedger";
import { deleteEquipmentLedger } from "@/api/assetManage/EquipmentLedger";
import { updEquipmentLedger } from "@/api/assetManage/EquipmentLedger";
import { GetTotaocount } from "@/api/assetManage/EquipmentLedger";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      tableData: [],
      dialogFormVisible: false,
      dialogTableVisible: false,
      dialogTableVisible1: false,
      multipleSelection: [],
      ids: {},
      form: {
        id: "",
        equipmentCoding: "",
        originalNum: "",
        equipmentName: "",
        equipmentLocaId: "",
        deviceType: "",
        detailedLocation: "",
        equipmentModel: "",
        equipmentBrand: "",
        purchaseDate: "",
        useDate: "",
        startTime: "",
        mainteCycle: "",
        mainteDepartId: "",
        personnelId: "",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        equipmentName: "",
        equipmentLocaId: "",
        equipmentName: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //二维码
    creatQrCode(row) {
      this.dialogTableVisible = true;
      $(".qrcode").html("");
      setTimeout(() => {
        new QRCode(this.$refs.qrCodeUrl, {
          text: row, // 需要转换为二维码的内容
          width: 260,
          height: 260,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
      }, 0);
    },
    //显示
    list() {
      GetShowEquipmentLedger(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deleteEquipmentLedger(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //修改
    examine(row) {
      this.dialogFormVisible1 = true;
      this.form = row;
    },
    baocun() {
      updEquipmentLedger(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible1 = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //跳转
    add() {
      this.$router.push("/assetManage/AddEquipmentLedger");
    },
  },
};
</script>
<style>
</style>